package edu.hziee.controllers;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import edu.hziee.dao.BlogDao;
import edu.hziee.model.Blog;

@Controller
@Transactional
@RequestMapping("/blog")
public class BlogController {

	@Autowired
	private BlogDao blogDao;
	
	//blog get
	@RequestMapping(method = RequestMethod.GET)
	public ModelAndView getBlogs(){
		ModelAndView mav = new ModelAndView("blog/blogList");
		
		List<Blog> blogs = blogDao.selectBlogs();
		mav.addObject("blogs",blogs);
		return mav;
	}
	
	//blog/{id} get
	@RequestMapping(value="/{id}", method = RequestMethod.GET)
	public ModelAndView getBlog(@PathVariable int id){
		ModelAndView mav = new ModelAndView("blog/blog");
		
		Blog blog = blogDao.selectBlog(id);
		mav.addObject("blog",blog);
		return mav;
	}
	
	//blog/add get
	@RequestMapping(value="/add", method = RequestMethod.GET)
	public ModelAndView addBlog(){
		ModelAndView mav = new ModelAndView("blog/addBlog");
		return mav;
	}
	
	//blog/add post
	@RequestMapping(value="/add", method = RequestMethod.POST)
	public ModelAndView addBlog(@RequestParam String title,
			@RequestParam String content,@RequestParam String author){
		ModelAndView mav = new ModelAndView();
		
		Blog blog = new Blog();
		blog.setAuthor(author);
		blog.setContent(content);
		blog.setTitle(title);
		
		blogDao.insertBlog(blog);
		
		mav.setViewName("redirect:/blog");
		return mav;
	}
	
	//blog/delete get
	@RequestMapping(value="/delete", method = RequestMethod.GET)
	public ModelAndView deleteBlog(@RequestParam int id){
		ModelAndView mav = new ModelAndView();
		
		blogDao.deleteBlog(id);
		
		mav.setViewName("redirect:/blog");
		return mav;
	}
	
	//blog/delete get
	@RequestMapping(value="/modify", method = RequestMethod.GET)
	public ModelAndView modifyBlog(@RequestParam int id){
		ModelAndView mav = new ModelAndView();
		
		Blog blog = blogDao.selectBlog(id);
		mav.addObject("blog", blog);
		
		mav.setViewName("blog/modifyBlog");
		return mav;
	}
	
	//blog/delete get
	@RequestMapping(value="/modify", method = RequestMethod.POST)
	public ModelAndView modifyBlogCommit(@RequestParam Long id,
			@RequestParam String title,
			@RequestParam String content,@RequestParam String author){
		ModelAndView mav = new ModelAndView();
		Blog blog = new Blog();
		blog.setId(id);
		blog.setTitle(title);
		blog.setAuthor(author);
		blog.setContent(content);
		blogDao.updateBlog(blog);
		
		mav.setViewName("redirect:/blog");
		return mav;
	}
}
